package server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import db.DBmanager;

/**
 * Servlet implementation class GetEvents
 */
public class GetEvents extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetEvents() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userName=request.getParameterValues("userName")[0];
		
		
		try {
			System.out.println("getting events");
			String [] events=DBmanager.getDB().getEventsForUser(userName);
			System.out.println("last 10 events"+events);
			Gson gson = new Gson();
			String regRes="";
			Locale loc= new Locale("he"); 
			response.setLocale(loc);
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out=response.getWriter();
			out.write(gson.toJson(events));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			PrintWriter out=response.getWriter();
			out.write("Problem while getting player's events");
			response.setStatus(400);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
